package com.zxy.ider.ehome.widget;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.content.Context;
import android.widget.Toast;

/**
 * 
* @ClassName: Utils 
* @Description: TODO(������һ�仰��������������) 
* @author zhaoyufei
* @date 2016-7-1 ����4:58:09
 */
public class Utils {
	private static Toast toast;
	/**
	 * 获取SDK版本
	 */
	public static int getSDKVersion() {
		int version = 0;
		try {
			version = Integer.valueOf(android.os.Build.VERSION.SDK);
		} catch (NumberFormatException e) {
		}
		return version;
	}
	public static void clear() {
		toast=null;
	}
	public static void showToast(Context context, String content) {
		if (toast == null) {
			toast = Toast.makeText(context, content, Toast.LENGTH_SHORT);
		} else {
			toast.setText(content);
		}
		toast.show();
	}
	public static void showToast(Context context, int stringId) {
		if (toast == null) {
			toast = Toast.makeText(context, stringId, Toast.LENGTH_SHORT);
		} else {
			toast.setText(context.getResources().getText(stringId));
		}
		toast.show();
	}
	
	
	/**
	 * Read snkey sn:32λ����
	 * 
	 */
	public static String readSN() {
		String nameNode = "/sys/class/unifykeys/name";
		String readNode = "/sys/class/unifykeys/read";

		writeSysFile(nameNode, "usid");
		String snString = readFile(readNode);
		return snString;
	}
	
	public static String readFile(String fileName) {
		String b = "";
		StringBuffer sb = new StringBuffer();
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new FileInputStream(fileName), "utf-8"));
			while ((b = br.readLine()) != null) { // ���ж�ȡ
				sb.append(b);
			}
			br.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();

	}
	
	public static boolean writeSysFile(String c, String v) {

		File file = new File(c);
		if (!file.exists()) {
			return false;
		}

		// write
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter(c), 4);
			try {
				out.write(v);
			} finally {
				out.close();
			}
			return true;
		} catch (IOException e) {
			return false;
		}
	}
	
	public static String getMd5ByFile(String inputFile) throws IOException {
	      // ��������С��������Գ��һ��������
	      int bufferSize = 2 * 1024 * 1024;
	      FileInputStream fileInputStream = null;
	      DigestInputStream digestInputStream = null;
	         // �õ�һ��MD5ת������ͬ����������Ի���SHA1��
	         MessageDigest messageDigest;
			try {
				messageDigest = MessageDigest.getInstance("MD5");
				// ʹ��DigestInputStream
		         fileInputStream = new FileInputStream(inputFile);
		         digestInputStream = new DigestInputStream(fileInputStream,messageDigest);
		         // read�Ĺ����н���MD5����ֱ�������ļ�
		         byte[] buffer =new byte[bufferSize];
		         while (digestInputStream.read(buffer) > 0);
		         // ��ȡ���յ�MessageDigest
		         messageDigest= digestInputStream.getMessageDigest();
		         // �õ������Ҳ���ֽ����飬����16��Ԫ��
		         BigInteger bi = new BigInteger(1, messageDigest.digest());
		         fileInputStream.close();
		         digestInputStream.close();
		         return bi.toString(16);
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return "";
	}
}
